package com.my.controller;

import cn.hutool.core.io.file.FileReader;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.entity.GradeClass;
import com.my.entity.NightSpot;
import com.my.service.GradeClassService;
import com.my.service.NightSpotService;
import com.my.util.execl.entity.NightSpotExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("nightSpot")
public class NightSpotController {

    @Autowired
    private NightSpotService service;

    @Autowired
    GradeClassService gcService;

    @GetMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") int pageNo, NightSpot nightSpot) {
        model.addAttribute("gradeList", gcService.list(new QueryWrapper<GradeClass>().eq("deep", 1)));
        model.addAttribute("uName", nightSpot.getUName());
        model.addAttribute("gId", nightSpot.getGId());
        model.addAttribute("cId", nightSpot.getCId());
        model.addAttribute("status", nightSpot.getStatus());
        model.addAttribute("startTime", nightSpot.getStartTime());
        model.addAttribute("endTime", nightSpot.getEndTime());
        model.addAttribute("page", service.getList(new Page<>(pageNo, 10), nightSpot));
        return "nightSpot/list";
    }

    @GetMapping("exportExcel")
    public void exportExcel(NightSpot spot, HttpServletResponse response) throws IOException {
        File file = null;
        try {
            file = File.createTempFile("学生晚点记录", ".xls");
        } catch (IOException e) {
            e.printStackTrace();

        }
        IPage<NightSpot> page = service.getList(new Page<>(1, 100000), spot);
        List<NightSpotExcel> spotExecls = new ArrayList<>();
        System.out.println(page.getRecords());
        page.getRecords().forEach(v -> {
            spotExecls.add(new NightSpotExcel(v.getUName(), v.getGName(), v.getCName(), (v.getStatus() == 1 ? "在宿舍" : (v.getStatus() == 2 ? "申请晚归" : "回家")), v.getRemarks(), v.getCreateTime()));
        });
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=download.xls");
        EasyExcel.write(file, NightSpotExcel.class).sheet().doWrite(spotExecls);
        ServletOutputStream out = response.getOutputStream();
        out.write(FileReader.create(file).readBytes());
        file.delete();
        out.close();
    }

}
